package com.sdlh.demo.springboot;

import com.sdlh.demo.springboot.po.OrderPO;
import com.sdlh.demo.springboot.po.RecordPO;
import com.sdlh.demo.springboot.service.OrderService;
import com.sdlh.demo.springboot.service.RecordService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Slf4j
@RestController
@RequestMapping("/testScope")
public class TestScopeController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RecordService recordService;

    @GetMapping(value = "/getOrder")
    public OrderPO getOrder(HttpServletRequest servletRequest) {
        String orderNum = servletRequest.getParameter("orderNum");
        log.info("请求{},参数orderNum={}", servletRequest.hashCode(), orderNum);
        log.info("orderService:{}", orderService);
        OrderPO order = orderService.findOrder(Long.parseLong(orderNum));
        log.info("threadId:{},order:{}", Thread.currentThread().getId(), order);
        return order;
    }

    @GetMapping(value = "/getRecord")
    public RecordPO getRecord(HttpServletRequest servletRequest) throws ExecutionException, InterruptedException {
        String item = servletRequest.getParameter("item");
        String table = servletRequest.getParameter("table");
        log.info("请求{},参数item={},table={}", servletRequest.hashCode(), item, table);
        log.info("recordService:{}", recordService);
        CompletableFuture<RecordPO> future = recordService.findItemAsync(Integer.parseInt(item), Integer.parseInt(table));
        RecordPO record = future.get();
        log.info("threadId:{},record:{}", Thread.currentThread().getId(), record);
        return record;
    }

    @GetMapping(value = "/login")
    public Object login(HttpServletRequest request) {
//        URI uri = URI.create("https://cn.fusionsolar.huawei.com/unisso/login.action#https%3A%2F%2Funi01cn.fusionsolar.huawei.com%2Funiportal%2Fpvmswebsite%2Fassets%2Fbuild%2Fcloud.html%3Fapp-id%3Dsmartpvms%26instance-id%3Dsmartpvms%26zone-id%3Ddfdd3e90-9ea1-4925-9b48-f3f7e99ac0d3%23%2Fhome%2Flist");
        URI uri = URI.create("https://uni01cn.fusionsolar.huawei.com/uniportal/pvmswebsite/assets/build/cloud.html?app-id=smartpvms&instance-id=smartpvms&zone-id=dfdd3e90-9ea1-4925-9b48-f3f7e99ac0d3#/home/list");
        String token = request.getHeader("XSRF-TOKEN");
        HttpRequest proxy_request = HttpRequest.newBuilder()
                                               .uri(uri)
                                               .header("XSRF-TOKEN", "n-hek6umfufvde1gdefulccaeqg45etiqr3vfyip6raolgju1jmqc4eo86dh49073s8bk9qktfqrvxrt6r9cur867sers68b7wnz3wunbw043tep497x85tf6ko8pi873v")
                                               .build();
        HttpResponse<String> proxy_response = null;

        try(HttpClient client = HttpClient.newBuilder()
                                          .build();
            FileOutputStream outputStream = new FileOutputStream("body.txt")
        ) {
            proxy_response = client.send(proxy_request,
                                         HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
            if (proxy_response.statusCode() == 200) {
                log.info("锦浪API[{}]调用成功，\n返回值为:{}", uri, proxy_response.body());
                IOUtils.write(proxy_response.body().getBytes(), outputStream);
            }

        } catch (Exception e) {
            log.error("请求锦浪云API{}调用出现异常:{}", uri, e.getMessage());
        }


        return proxy_response;
    }
}
